package com.attendance.www.hr.service.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;

import com.attendance.www.hr.beans.AttachmentBean;
import com.attendance.www.hr.constant.AppConstant;
import com.attendance.www.hr.dao.mapper.AttachmentMapper;
import com.attendance.www.hr.entitys.Attachment;
import com.attendance.www.hr.exception.ResultFaultException;
import com.attendance.www.hr.service.IEmployeeCorporationExcelService;
import com.attendance.www.hr.service.IEmployeeDepartmentExcelService;
import com.attendance.www.hr.service.IEmployeeExcelService;
import com.attendance.www.hr.service.IEmployeeImportService;
import com.attendance.www.hr.service.IEmployeeRoleExcelService;
import com.attendance.www.hr.service.IUserExcelService;



@Service
public class EmployeeImportServiceImpl implements IEmployeeImportService {

	@Resource
	private AttachmentMapper attachmentDao;

	@Resource
	private IEmployeeExcelService employeeExcelService;
	@Resource
	private IEmployeeCorporationExcelService employeeCorporationExcelService;
	@Resource
	private IEmployeeDepartmentExcelService employeeDepartmentExcelService;
	@Resource
	private IEmployeeRoleExcelService employeeRoleExcelService;
	@Resource
	private IUserExcelService userExcelService;
	
	/**
	 * 导入基础数据库
	 */
	@Override
	public List<String> importCode(AttachmentBean attachmentBean) throws Exception {
		List<String> successList =new ArrayList<String>();
		// 查询附件表获取路径地址
		Attachment attachment = attachmentDao.selectByPrimaryKey(attachmentBean.getAttachmentId());
		// 设置文件路径
		String pathName = attachment.getUrl().replace(AppConstant.UPLOAD_RESOURCE, AppConstant.SAVE_FILE_REPOSITORY);
		File file = new File(pathName);
		Workbook work = null;
		if (file.exists()) {
			try {
				InputStream inputStream = new FileInputStream(file);
				if (pathName.matches("^.+\\.(?i)(xls)$")) {
					work = new HSSFWorkbook(inputStream);
				} else if (pathName.matches("^.+\\.(?i)(xlsx)$")) {
					work = new XSSFWorkbook(inputStream);
				}
			} catch (Exception ex) {
				throw new ResultFaultException("0_00003");
			}
		} else {
			throw new ResultFaultException("0_00003");
		}
		if (work == null) {
			throw new ResultFaultException("0_00003");
		}
		
		if(work != null){
			//员工导入
			List<String> employeeList=employeeExcelService.importEmployee(work, attachmentBean);
			if(employeeList.size()>0){
				successList.addAll(employeeList);
			}
			//员工与公司关系导入
			List<String> employeeCorporationList=employeeCorporationExcelService.importEmployeeCorporation(work, attachmentBean);
			if(employeeCorporationList.size()>0){
				successList.addAll(employeeCorporationList);
			}
			//员工与部门关系导入
			List<String> employeeDepartmentList=employeeDepartmentExcelService.importEmployeeDepartment(work, attachmentBean);
			if(employeeDepartmentList.size()>0){
				successList.addAll(employeeDepartmentList);
			}
			//员工与岗位关系导入
			List<String> employeeRoleExcelList=employeeRoleExcelService.importEmployeeRole(work, attachmentBean);
			if(employeeRoleExcelList.size()>0){
				successList.addAll(employeeRoleExcelList);
			}
		}
		return successList;
	}
	
	/**
	 * 清除组织架构信息
	 * @return
	 */
	@SuppressWarnings("unused")
	private boolean delOrganizationalStructure(){
//		//删除项目相关数据
//		projectExcelService.delProject();
//		//删除客户数据
//		customerExcelService.delCustomer();
//		//删除公司相关数据
//		corporationExcelService.delCorporation();
//		//删除业务线相关数据
//		businessUnitExcelService.delBusinessUnit();
//		//删除产品相关数据
//		productExcelService.delProduct();
//		//删除岗位相关数据
//		roleExcelService.delRole();
//		//删除部门相关数据
//		departmentExcelService.delDepartment();
//		//删除成本项导入
//		costitemExcelService.delCostitem();
		return true;
	}
}
